<?php

class admincp_mail{

	var $adminid, $systems, $title, $detail, $receiverids;

	public function create(){
		$mail = array(
			'adminid' => $this->adminid,
			'title' => $this->title,
			'detail' => $this->detail,
			'system' => $this->systems ? 1 : 0,
			'create_time' => SYS_TIME
		);
		if( $this->receiverids ){
			$mailid = DB::Insert('admincp_mail', $mail);
			if( $mailid ){
				$this->receiverids = is_array($this->receiverids) ? $this->receiverids : explode(',', $this->receiverids);
				$sents = array();
				foreach( $this->receiverids as $k => $v ){
					$sents[$v] = $this->send($mailid, $v);
				}

				$ids = join('\',\'', $this->receiverids);
				if( $ids ){
					$admins = limitquery('admincp_member', array("`id` IN('{$ids}')"));
					foreach( $admins as $k => $v ){
						DMail::SMTP('yangkun@heivr.com', 'yangkun@heivr.com', $this->title, $this->detail);
					}
				}

				return count($sents);
			}
		}
		return false;
	}

	public function send($mailid = 0, $receiverid = 0){
		return DB::Insert('admincp_message', array(
			'mailid' => $mailid,
			'receiverid' => $receiverid,
			'create_time' => SYS_TIME
		));
	}
}

?>